package models

import (
	"time"

	"github.com/astaxie/beego/orm"
	"read.com/admin/libs"
	"fmt"
	"encoding/base64"
)

type MassTask struct {
	Id       int       `orm:"column(id);auto" description:"ID"`
	SendDate time.Time `orm:"column(send_date);type(date)" description:"发布日期"`
	SendTime int8      `orm:"column(send_time)" description:"发布时间"`
	AppId    int       `orm:"column(app_id)" description:"应用ID"`
	SendType int8      `orm:"column(send_type)" description:"发布形式"`
	IsSend   int8      `orm:"column(is_send)" description:"是否已发送"`
	HeadText string    `orm:"column(head_text);size(255);null" description:"头"`
	TailText string    `orm:"column(tail_text);size(255);null" description:"尾"`
}

func (t *MassTask) TableName() string {
	return "mass_task"
}

func init() {
	orm.RegisterModel(new(MassTask))
}

type FormatMassTask struct {
	Sort    int
	AppName string
	MassTask
}

func GetMassTaskList(appId int, date string, page int, pageSize int) ([]*FormatMassTask, int64) {
	o := orm.NewOrm()
	cond := orm.NewCondition()
	if appId != -1 {
		cond = cond.And("app_id", appId)
	}
	if date != "" {
		sendDate := libs.StrToTimeDay(date)
		cond = cond.And("send_date", sendDate)
	}
	offset := (page - 1) * pageSize
	massTasks := make([]*MassTask, 0)
	query := o.QueryTable(TableName("mass_task"))
	query = query.SetCond(cond)
	total, _ := query.Count()
	query.OrderBy("-send_date").Limit(pageSize, offset).All(&massTasks)

	formatMassTasks := make([]*FormatMassTask, 0)
	for k, v := range massTasks {
		appName := ""
		if app, find := appHash[v.AppId]; find {
			appName = app.AppName
		}
		formatMassTask := &FormatMassTask{
			Sort:     k + 1,
			AppName:  appName,
			MassTask: *v,
		}
		headText,_ := base64.StdEncoding.DecodeString(formatMassTask.HeadText)
		tailText,_ := base64.StdEncoding.DecodeString(formatMassTask.TailText)
		formatMassTask.HeadText = string(headText)
		formatMassTask.TailText = string(tailText)
		formatMassTasks = append(formatMassTasks, formatMassTask)
	}
	return formatMassTasks, total
}

func AddTask(appId int,sendDate string,sendTime int8,sendType int8,headText string,tailText string) error{
	o := orm.NewOrm()
	massTask := &MassTask{
		SendDate:libs.StrToTimeDay(sendDate),
		SendTime:sendTime,
		AppId:appId,
		SendType:sendType,
		HeadText:base64.StdEncoding.EncodeToString([]byte(headText)),
		TailText:base64.StdEncoding.EncodeToString([]byte(tailText)),
	}
	_,err := o.Insert(massTask)
	if err != nil {
		fmt.Println("insert mass_task error",err)
		return err
	}
	return nil
}

func EditTask(id int,sendDate string,sendTime int8,sendType int8,headText string,tailText string) error{
	o := orm.NewOrm()
	massTask := &MassTask{Id:id}
	err := o.Read(massTask)
	if err != nil {
		fmt.Println("mass_task not found", err)
		return err
	}
	massTask.SendDate = libs.StrToTimeDay(sendDate)
	massTask.SendTime = sendTime
	massTask.SendType = sendType
	massTask.HeadText = base64.StdEncoding.EncodeToString([]byte(headText))
	massTask.TailText = base64.StdEncoding.EncodeToString([]byte(tailText))
	if _, err := o.Update(massTask); err != nil {
		fmt.Println("update mass_task error", err)
		return err
	}
	return nil
}

func DelTask(id int) error{
	o := orm.NewOrm()
	if _, err := o.Delete(&MassTask{Id: id}); err != nil {
		fmt.Println("delete mass_task error", err)
		return err
	}
	_, err := o.Raw("delete from mass_item where task_id = ?", id).Exec()
	if err != nil {
		fmt.Println("delete mass_item error", err)
		return err
	}
	return nil
}